﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F4CheckRV201F06 : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private DataSet ds;
        private DataViewManager dsView;

        private Dictionary<string, string> required_list;

        public frm_Add_F4CheckRV201F06()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("RobotOrderDetail");

            this.required_list = new Dictionary<string, string>() { { "serialNo", "Serial No" }, { "writtenBy", "Written By" },
            { "serial_No_RJ", "Serial No" }, { "checkBy", "Check By"}, {"writtenDate", "Written Date"}, {"exportDate", "Export Date"},
            {"customerID", "Customer"}, {"appDiagram", "Appearance Diagram"}, {"wiringDiagram", "Wiring Diagram"},
            {"RC_510_Serial", "RC-510 Serial"}, {"SB020_Serial", "SB020 Serial"}, {"SB017A_Serial", "SB017A Serial"},
            {"Indicator1_Serial", "Indicator1 Serial"}, {"SB019_Door_Serial", "SB019(Door) Serial"},
            {"SB019_Stg1_Serial", "SB019(Stg1) Serial"}, {"SB019_Stg2_Serial", "SB019(Stg2) Serial"},
            {"Y_Axis_Driver_Serial", "Y Axis Driver Serial"}, {"Z_Axis_Driver_Serial", "Z Axis Driver Serial"},
            {"CID_Serial", "CID Serial"}, {"RC_510_Program_File", "RC-510 Program File"}, {"Program_Version", "Program Version"},
            {"FPGA_Version", "FPGA Version"}, {"Initialize_Data_File", "Initialize Data File"}, {"Body_No", "Body No"},
            {"IP_Address", "IP Address"},
            {"MAC_Address1", "MAC Address 1"}, {"MAC_Address2", "MAC Address 1"}, {"MAC_Address3", "MAC Address 1"},
            {"Y_Position1", "Y Axis Position1"}, {"Y_Position2", "Y Axis Position2"}, {"Y_Position3", "Y Axis Position3"},
            {"Y_Position4", "Y Axis Position4"}, {"Y_Position5", "Y Axis Position5"}, {"Y_Position6", "Y Axis Position6"},
            {"Z_Position1", "Z Axis Position1"}, {"Z_Position2", "Z Axis Position2"}, {"Z_Position3", "Z Axis Position3"},
            {"Z_Position4", "Z Axis Position4"}, {"Z_Position5", "Z Axis Position5"}, {"Z_Position6", "Z Axis Position6"},
            {"Mapping_Top", "Mapping Top"}, {"Mapping_Bottom", "Mapping Bottom"}, {"BRC_Reading", "BRC Reading"},
            {"Stacker_Hand_Threshold", "Stacker Hand Threshold"}, {"Laser_Print_Threshold", "Laser Print Threshold"},
            {"YAX1_Driver_Ver", "YAX1 Driver Ver"}, {"ZAX1_Driver_Ver", "ZAX1 Driver Ver"}, {"RIO_Door_Ver1", "RIO/Door Ver1"},
            {"RIO_Stage_Ver1", "RIO/Stage Ver1"}, {"RIO_Stage_Ver2", "RIO/Stage Ver2"}, {"RIO_Indicator1_Ver1", "RIO/Indicator1 Ver1"},
            {"RIO_Indicator1_Ver2", "RIO/Indicator1 Ver2"}, {"CPLD0_Ver", "CPLD0 Ver"}, {"CPLD1_Ver", "CPLD1 Ver"},
            {"LoaderLD_510_Ver", "Loader LD-510 Ver"}};
            
            LoadSerialList();
            LoadCustomerList();
            LoadStaffList();
            LoadDefaultData();            
        }

        ////////////////////////////////////////////////////
        /// Load Serial List
        ////////////////////////////////////////////////////
        private void LoadSerialList()
        {
            
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE(SerialNo LIKE N'E%') AND SerialNo NOT IN (SELECT [Serial No(RRC)] COLLATE Vietnamese_CI_AS FROM [231_RV201F06])";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "RobotOrderDetail");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.serialNo.DataSource = dsView;
                this.serialNo.DisplayMember = "RobotOrderDetail.SerialNo";
                this.serialNo.ValueMember = "RobotOrderDetail.SerialNo";
                this.serialNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partNo.DataBindings.Add("Text", dsView, "RobotOrderDetail.CompID");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            } 
        }

        ////////////////////////////////////////////////////
        /// Load Customer List
        ////////////////////////////////////////////////////
        private void LoadCustomerList()
        {

            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [CustomerID] FROM [231_Customer]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CustomerID"].ToString().Trim();
                    item.Text = dataReader["CustomerID"].ToString().Trim();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Staff List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();                    
                    item.Value = txt + dataReader["StaffID"].ToString().Trim();

                    this.writtenBy.Properties.Items.Add(item);
                    this.checkBy.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////////////////////
        /// Load Default Data
        ////////////////////////////////////////////////////
        private void LoadDefaultData()
        {
            
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [Appearance Diagram], [Wiring Diagram], [RC-510 Program File],"
                    + " [Program Version], [FPGA Version], [Initialize Data File], [IP Adress], [YAX1 Driver Ver],"
                    + " [ZAX1 Driver Ver], [RIO/Door Ver 1], [RIO/Stage Ver 1], [RIO/Stage Ver 2],"
                    + " [RIO/Indicator1 Ver 1], [RIO/Indicator1 Ver 2], [CPLD0 Ver], [CPLD1 Ver], [Loader LD-510 Ver]"
                    + " FROM [231_RV201F06] GROUP BY [Appearance Diagram], [Wiring Diagram], [RC-510 Program File],"
                    + " [Program Version], [FPGA Version], [Initialize Data File], [IP Adress], [YAX1 Driver Ver],"
                    + " [ZAX1 Driver Ver], [RIO/Door Ver 1], [RIO/Stage Ver 1], [RIO/Stage Ver 2], [RIO/Indicator1 Ver 1],"
                    + " [RIO/Indicator1 Ver 2], [CPLD0 Ver], [CPLD1 Ver], [Loader LD-510 Ver]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item1 = new ComboboxItem();
                    item1.Value = dataReader["Appearance Diagram"].ToString().Trim();
                    item1.Text = dataReader["Appearance Diagram"].ToString().Trim();
                    this.appDiagram.Properties.Items.Add(item1);

                    ComboboxItem item2 = new ComboboxItem();
                    item2.Value = dataReader["Wiring Diagram"].ToString().Trim();
                    item2.Text = dataReader["Wiring Diagram"].ToString().Trim();
                    this.wiringDiagram.Properties.Items.Add(item2);

                    ComboboxItem item3 = new ComboboxItem();
                    item3.Value = dataReader["RC-510 Program File"].ToString().Trim();
                    item3.Text = dataReader["RC-510 Program File"].ToString().Trim();
                    this.RC_510_Program_File.Properties.Items.Add(item3);

                    ComboboxItem item4 = new ComboboxItem();
                    item4.Value = dataReader["Program Version"].ToString().Trim();
                    item4.Text = dataReader["Program Version"].ToString().Trim();
                    this.Program_Version.Properties.Items.Add(item4);

                    ComboboxItem item5 = new ComboboxItem();
                    item5.Value = dataReader["FPGA Version"].ToString().Trim();
                    item5.Text = dataReader["FPGA Version"].ToString().Trim();
                    this.FPGA_Version.Properties.Items.Add(item5);

                    ComboboxItem item6 = new ComboboxItem();
                    item6.Value = dataReader["Initialize Data File"].ToString().Trim();
                    item6.Text = dataReader["Initialize Data File"].ToString().Trim();
                    this.Initialize_Data_File.Properties.Items.Add(item6);

                    ComboboxItem item7 = new ComboboxItem();
                    item7.Value = dataReader["IP Adress"].ToString().Trim();
                    item7.Text = dataReader["IP Adress"].ToString().Trim();
                    this.IP_Address.Properties.Items.Add(item7);

                    ComboboxItem item8 = new ComboboxItem();
                    item8.Value = dataReader["YAX1 Driver Ver"].ToString().Trim();
                    item8.Text = dataReader["YAX1 Driver Ver"].ToString().Trim();
                    this.YAX1_Driver_Ver.Properties.Items.Add(item8);

                    ComboboxItem item9 = new ComboboxItem();
                    item9.Value = dataReader["ZAX1 Driver Ver"].ToString().Trim();
                    item9.Text = dataReader["ZAX1 Driver Ver"].ToString().Trim();
                    this.ZAX1_Driver_Ver.Properties.Items.Add(item9);

                    ComboboxItem item10 = new ComboboxItem();
                    item10.Value = dataReader["RIO/Door Ver 1"].ToString().Trim();
                    item10.Text = dataReader["RIO/Door Ver 1"].ToString().Trim();
                    this.RIO_Door_Ver1.Properties.Items.Add(item10);

                    ComboboxItem item11 = new ComboboxItem();
                    item11.Value = dataReader["RIO/Stage Ver 1"].ToString().Trim();
                    item11.Text = dataReader["RIO/Stage Ver 1"].ToString().Trim();
                    this.RIO_Stage_Ver1.Properties.Items.Add(item11);

                    ComboboxItem item12 = new ComboboxItem();
                    item12.Value = dataReader["RIO/Stage Ver 2"].ToString().Trim();
                    item12.Text = dataReader["RIO/Stage Ver 2"].ToString().Trim();
                    this.RIO_Stage_Ver2.Properties.Items.Add(item12);

                    ComboboxItem item13 = new ComboboxItem();
                    item13.Value = dataReader["RIO/Indicator1 Ver 1"].ToString().Trim();
                    item13.Text = dataReader["RIO/Indicator1 Ver 1"].ToString().Trim();
                    this.RIO_Indicator1_Ver1.Properties.Items.Add(item13);

                    ComboboxItem item14 = new ComboboxItem();
                    item14.Value = dataReader["CPLD0 Ver"].ToString().Trim();
                    item14.Text = dataReader["CPLD0 Ver"].ToString().Trim();
                    this.CPLD0_Ver.Properties.Items.Add(item14);

                    ComboboxItem item15 = new ComboboxItem();
                    item15.Value = dataReader["CPLD1 Ver"].ToString().Trim();
                    item15.Text = dataReader["CPLD1 Ver"].ToString().Trim();
                    this.CPLD1_Ver.Properties.Items.Add(item15);

                    ComboboxItem item16 = new ComboboxItem();
                    item16.Value = dataReader["RIO/Indicator1 Ver 2"].ToString().Trim();
                    item16.Text = dataReader["RIO/Indicator1 Ver 2"].ToString().Trim();
                    this.RIO_Indicator1_Ver2.Properties.Items.Add(item16);

                    ComboboxItem item17 = new ComboboxItem();
                    item17.Value = dataReader["Loader LD-510 Ver"].ToString().Trim();
                    item17.Text = dataReader["Loader LD-510 Ver"].ToString().Trim();
                    this.LoaderLD_510_Ver.Properties.Items.Add(item17);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();

                myConnection.Open();
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_F5Detail]"
                    + " WHERE ([FinishDate] > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) - 500)";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    item.Text = dataReader["SerialNo"].ToString().Trim();

                    this.RC_510_Serial.Properties.Items.Add(item);
                    this.SB020_Serial.Properties.Items.Add(item);
                    this.SB017A_Serial.Properties.Items.Add(item);
                    this.Indicator1_Serial.Properties.Items.Add(item);                    
                    this.SB019_Door_Serial.Properties.Items.Add(item);
                    this.SB019_Stg1_Serial.Properties.Items.Add(item);                    
                    this.SB019_Stg2_Serial.Properties.Items.Add(item);
                    this.Y_Axis_Driver_Serial.Properties.Items.Add(item);
                    this.Z_Axis_Driver_Serial.Properties.Items.Add(item);                    
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            } 
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            if (this.serialNo.SelectedIndex < 0)
            {
                error = "Please select Serial No.";
            }

            if (string.IsNullOrEmpty(error))
            {
                foreach (Control c in this.Controls)
                {
                    if (c is System.Windows.Forms.ComboBox)
                    {
                        if ((((System.Windows.Forms.ComboBox)c).SelectedIndex < 0) && string.IsNullOrEmpty(((System.Windows.Forms.ComboBox)c).Text))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.ComboBox)c).Name))
                            {
                                error = "Please select " + required_list[((System.Windows.Forms.ComboBox)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.ComboBoxEdit)
                    {
                        if ((((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex < 0) && string.IsNullOrEmpty(((DevExpress.XtraEditors.ComboBoxEdit)c).Text))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)c).Name))
                            {
                                error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.TextEdit)
                    {
                        if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)c).Name))
                            {
                                error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.TextBox)
                    {
                        if (string.IsNullOrEmpty(((System.Windows.Forms.TextBox)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.TextBox)c).Name))
                            {
                                error = "Please enter " + required_list[((System.Windows.Forms.TextBox)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.GroupBox)
                    {
                        foreach (Control sub_c in c.Controls)
                        {
                            if (sub_c is DevExpress.XtraEditors.TextEdit)
                            {
                                if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)sub_c).Text.Trim()))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)sub_c).Name))
                                    {
                                        error = "Plaase enter " + required_list[((DevExpress.XtraEditors.TextEdit)sub_c).Name];
                                    }
                                }
                            }

                            if (sub_c is DevExpress.XtraEditors.ComboBoxEdit)
                            {
                                if ((((DevExpress.XtraEditors.ComboBoxEdit)sub_c).SelectedIndex < 0) && string.IsNullOrEmpty(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Text))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name))
                                    {
                                        error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {                   
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "INSERT INTO [231_RV201F06] ([Serial No(RJ)], [Comp No#], [Serial No(RRC)],"
                        + " [Written(Name)], [Written(No#)], [Checked(Name)], [Checked(No#)], [Written Year], [Written Month],"
                        + " [Written Day], [Export Year], [Export Month], [Export Day], [Customer], [Remark],"
                        + " [Appearance Diagram], [Wiring Diagram], [RC-510 Serial], [SB020 Serial], [SB017A Serial],"
                        + " [Indicator1 Serial], [SB019(Door) Serial], [<Reserved>], [<Reserved>1], [<Reserved>2],"
                        + " [SB019(Stg1) Serial], [SB019(Stg2) Serial], [<Reserved>3], [<Reserved>4], [Indiactor2 Serial],"
                        + " [<Reserved>5], [<Reserved>6], [<Reserved>7], [Y-Axis Driver Serial], [Z-Axis Driver Serial],"
                        + " [CID Serial], [RC-510 Program File], [Program Version], [FPGA Version], [Initialize Data File],"
                        + " [Body No], [IP Adress], [MAC Address 1], [MAC Address 2], [MAC Address 3], [Y-Axis Position 1],"
                        + " [Y-Axis Position 2], [Y-Axis Position 3], [Y-Axis Position 4], [Y-Axis Position 5],"
                        + " [Y-Axis Position 6], [Z-Axis Position 1], [Z-Axis Position 2], [Z-Axis Position 3],"
                        + " [Z-Axis Position 4], [Z-Axis Position 5], [Z-Axis Position 6], [Mapping Top], [Mapping Bottom],"
                        + " [BCR Reading], [Stacker Hand Threshold], [Laser Prt# Threshold], [YAX1 Driver Ver],"
                        + " [ZAX1 Driver Ver], [RIO/Door Ver 1], [RIO/Door Ver 2], [RIO/Door Ver 3], [RIO/Door Ver 4],"
                        + " [RIO/Stage Ver 1], [RIO/Stage Ver 2], [RIO/Stage Ver 3], [RIO/Stage Ver 4], [RIO/Indicator1 Ver 1],"
                        + " [RIO/Indicator1 Ver 2], [RIO/Indicator1 Ver 3], [RIO/Indicator1 Ver 4], [RIO/Indicator2 Ver 1],"
                        + " [RIO/Indicator2 Ver 2], [RIO/Indicator2 Ver 3], [RIO/Indicator2 Ver 4], [CPLD0 Ver], [CPLD1 Ver],"
                        + " [Loader LD-510 Ver])"
                        + " VALUES ('" + this.serial_No_RJ.Text.Trim() + "', '" + this.partNo.Text.Trim() + "',"
                        + " '" + this.serialNo.SelectedValue.ToString() + "', '" + this.writtenName.Text.Trim() + "',"
                        + " '" + this.writtenID.Text.Trim() + "', '" + this.checkName.Text.Trim() + "',"
                        + " '" + this.checkID.Text.Trim() + "', '" + this.writtenYear.Text.Trim() + "',"
                        + " '" + this.writtenMonth.Text.Trim() + "', '" + this.writtenDay.Text.Trim() + "',"
                        + " '" + this.exportYear.Text.Trim() + "', '" + this.exportMonth.Text.Trim() + "',"
                        + " '" + this.exportDay.Text.Trim() + "', '" + this.customerID.Text.Trim() + "',"
                        + " '" + this.Remark.Text.Trim() + "', '" + this.appDiagram.Text.Trim() + "',"
                        + " '" + this.wiringDiagram.Text.Trim() + "', NULLIF('" + this.RC_510_Serial.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.SB020_Serial.Text.Trim() + "', ''), NULLIF('" + this.SB017A_Serial.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Indicator1_Serial.Text.Trim() + "', ''), NULLIF('" + this.SB019_Door_Serial.Text.Trim() + "', ''),"
                        + " '***', '***', '***', NULLIF('" + this.SB019_Stg1_Serial.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.SB019_Stg2_Serial.Text.Trim() + "', ''), '***', '***',"
                        + " '***', '***', '***', '***', NULLIF('" + this.Y_Axis_Driver_Serial.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_Axis_Driver_Serial.Text.Trim() + "', ''), NULLIF('" + this.CID_Serial.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.RC_510_Program_File.Text.Trim() + "', ''), NULLIF('" + this.Program_Version.Text.Trim() + "', ''), "
                        + " NULLIF('" + this.FPGA_Version.Text.Trim() + "', ''), NULLIF('" + this.Initialize_Data_File.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Body_No.Text.Trim() + "', ''), NULLIF('" + this.IP_Address.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.MAC_Address1.Text.Trim() + "', ''), NULLIF('" + this.MAC_Address2.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.MAC_Address3.Text.Trim() + "', ''), NULLIF('" + this.Y_Position1.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Y_Position2.Text.Trim() + "', ''), NULLIF('" + this.Y_Position3.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Y_Position4.Text.Trim() + "', ''), NULLIF('" + this.Y_Position5.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Y_Position6.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_Position1.Text.Trim() + "', ''), NULLIF('" + this.Z_Position2.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_Position3.Text.Trim() + "', ''), NULLIF('" + this.Z_Position4.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_Position5.Text.Trim() + "', ''), NULLIF('" + this.Z_Position6.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Mapping_Top.Text.Trim() + "', ''), NULLIF('" + this.Mapping_Bottom.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.BRC_Reading.Text.Trim() + "', ''), NULLIF('" + this.Stacker_Hand_Threshold.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Laser_Print_Threshold.Text.Trim() + "', ''), NULLIF('" + this.YAX1_Driver_Ver.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.ZAX1_Driver_Ver.Text.Trim() + "', ''), NULLIF('" + this.RIO_Door_Ver1.Text.Trim() + "', ''),"
                        + " '***', '***', '***', NULLIF('" + this.RIO_Stage_Ver1.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.RIO_Stage_Ver2.Text.Trim() + "', ''), '***', '***',"
                        + " NULLIF('" + this.RIO_Indicator1_Ver1.Text.Trim() + "', ''), NULLIF('" + this.RIO_Indicator1_Ver2.Text.Trim() + "', ''),"
                        + " '***', '***', '***', '***', '***', '***', NULLIF('" + this.CPLD0_Ver.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.CPLD1_Ver.Text.Trim() + "', ''), NULLIF('" + this.LoaderLD_510_Ver.Text.Trim() + "', ''))";                    
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Database updated successfully!");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void writtenBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.writtenBy.SelectedIndex >= 0)
            {
                this.writtenName.Text = this.writtenBy.Text.Substring(0, this.writtenBy.Text.Length - 4);
                this.writtenID.Text = this.writtenBy.Text.Substring(this.writtenBy.Text.Length - 4, 4);
            }
        }

        private void checkBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.checkBy.SelectedIndex >= 0)
            {
                this.checkName.Text = this.checkBy.Text.Substring(0, this.checkBy.Text.Length - 4);
                this.checkID.Text = this.checkBy.Text.Substring(this.checkBy.Text.Length - 4, 4);
            }
        }

        private void writtenDate_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.writtenDate.EditValue.ToString()))
            {
                this.writtenYear.Text = DateTime.Parse(this.writtenDate.EditValue.ToString()).Year.ToString("D4");
                this.writtenMonth.Text = DateTime.Parse(this.writtenDate.EditValue.ToString()).Month.ToString("D2");
                this.writtenDay.Text = DateTime.Parse(this.writtenDate.EditValue.ToString()).Day.ToString("D2");
            }
        }

        private void exportDate_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.exportDate.EditValue.ToString()))
            {
                this.exportYear.Text = DateTime.Parse(this.exportDate.EditValue.ToString()).Year.ToString("D4");
                this.exportMonth.Text = DateTime.Parse(this.exportDate.EditValue.ToString()).Month.ToString("D2");
                this.exportDay.Text = DateTime.Parse(this.exportDate.EditValue.ToString()).Day.ToString("D2");
            }
        }

        private void serialNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.serialNo.Text.Substring(this.serialNo.Text.Length - 1, 1).ToUpper() == "F")
            {
                this.serial_No_RJ.Text = this.serialNo.Text.Substring(0, this.serialNo.Text.Length - 1);
            }
            else
            {
                this.serial_No_RJ.Text = this.serialNo.Text;
            }
        }

        private void RC_510_Serial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.RC_510_Serial.SelectedIndex >= 0)
            {
                if (this.RC_510_Serial.Text.Length >= 7)
                {
                    this.MAC_Address1.Text = this.RC_510_Serial.Text.Substring(1, 2);
                    this.MAC_Address2.Text = this.RC_510_Serial.Text.Substring(3, 2);
                    this.MAC_Address3.Text = this.RC_510_Serial.Text.Substring(5, 2);
                }
            }
        }
    }
}
